<html>
<head><meta charset="utf-8"><title>folding in vscode · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html">folding in vscode</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="178660559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178660559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178660559">(Oct 21 2019 at 14:30)</a>:</h4>
<p>Hi <span class="user-mention" data-user-id="133169">@matklad</span> are you available for a quick chat on <a href="https://github.com/rust-analyzer/rust-analyzer/issues/2033" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/2033">https://github.com/rust-analyzer/rust-analyzer/issues/2033</a> ?</p>



<a name="178660656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178660656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178660656">(Oct 21 2019 at 14:31)</a>:</h4>
<p>yeah!</p>



<a name="178661345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178661345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178661345">(Oct 21 2019 at 14:39)</a>:</h4>
<p>Nice, so it turns out folding in vscode is pretty terrible with complaints going all the way to 2016 :) Basically it will slurp up the full last line of the folding range. First thing I tried was excluding it unconditionally - but the downside is that folds of modules and comments start looking really strange. Second idea - exclude the last line only if the folding range ends somewhere before the line end. This kind of works (see <a href="https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517" target="_blank" title="https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517">https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517</a>) modulo corner cases like trailing whitespace. Today I had another idea - add a field named something like <code>next_non_whitespace_elt_start</code> to <code>struct Fold</code> and calculate it along with the folding regions (where the full syntax tree is available). Seems a bit strange to do it only for that purpose but looks like it is the cleanest way. What do you think?</p>



<a name="178661863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178661863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178661863">(Oct 21 2019 at 14:44)</a>:</h4>
<p>Interesting!</p>
<p>I think it's not the best choice to put "this this otherwise blank line?" logic to the code that computes folding regions themselves. THe code in ra_ide_api is expcted to be independent of the LSP layer. That is, the hope is that it will be usable even without LSP.</p>
<p>So, unless it is absolutely horrible, I would prefer to do this calculation in the conv layer. One thing which might help here is to actually pass in the text of the file to conv? That way, you can do something like <code>text[fold..].chars().take_while(|it| it !='\n').filter(|it| !it.is_whitespace()).count() &gt; 0</code></p>



<a name="178661986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178661986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178661986">(Oct 21 2019 at 14:45)</a>:</h4>
<p>And I think that we want to check if fold is also the <em>start</em> of the line?</p>



<a name="178662078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662078">(Oct 21 2019 at 14:46)</a>:</h4>
<p>but in general, patch <a href="https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517" target="_blank" title="https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517">https://github.com/ztlpn/rust-analyzer/commit/173ac3d367e26c4ceb20f24b62e25f78a94e4517</a> looks good to me</p>



<a name="178662144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662144">(Oct 21 2019 at 14:47)</a>:</h4>
<p>Like, it's not the most pleasant code I've seen, but it is restricted strictly to this thin conversion layer, so supporting it shouldn't be too bad.</p>



<a name="178662297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662297">(Oct 21 2019 at 14:49)</a>:</h4>
<p>Huh... I didn't realize that folding range took line endings into account</p>



<a name="178662393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662393">(Oct 21 2019 at 14:50)</a>:</h4>
<blockquote>
<p>One thing which might help here is to actually pass in the text of the file to conv?</p>
</blockquote>
<p>Yeah, that would help. The question is - how to get it? A separate query?</p>



<a name="178662422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662422">(Oct 21 2019 at 14:50)</a>:</h4>
<p>Yeah, that's one bit that seemed a little eyebrow-rasing for me. I think if we switch to looking at the text directly, than we wouldn't need to look at line endings</p>



<a name="178662452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662452">(Oct 21 2019 at 14:51)</a>:</h4>
<p><span class="user-mention" data-user-id="245627">@ztlpn</span> <code>Analysis::file_text</code></p>



<a name="178662507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662507">(Oct 21 2019 at 14:51)</a>:</h4>
<p>Ok, good</p>



<a name="178662564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662564">(Oct 21 2019 at 14:52)</a>:</h4>
<p>Another question - how do I test it?</p>



<a name="178662594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662594">(Oct 21 2019 at 14:52)</a>:</h4>
<p>heavy test seems a bit, well, heavy</p>



<a name="178662790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178662790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178662790">(Oct 21 2019 at 14:54)</a>:</h4>
<p><span class="user-mention" data-user-id="245627">@ztlpn</span> in general, we don't test conv layer heavily: what matters is how the over side interprets converted things, so tests are not super-helpful in that case.</p>
<p>Here', I'd extract this into a helper function and test that</p>
<div class="codehilite"><pre><span></span>fn fixup_folding_ranges_for_editors_which_cant_fold_lines_partially(
    folds: &amp;mut [Fold],
    file_text: &amp;str
) {
    ....
}
</pre></div>



<a name="178663083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178663083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178663083">(Oct 21 2019 at 14:57)</a>:</h4>
<p>Sure</p>



<a name="178663086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178663086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178663086">(Oct 21 2019 at 14:57)</a>:</h4>
<p>Thanks</p>



<a name="178663765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178663765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178663765">(Oct 21 2019 at 15:03)</a>:</h4>
<p><span class="user-mention" data-user-id="245627">@ztlpn</span> this might be confusing, so let me explicitelly mention just in case: the text you get from  <code>file_text</code> is guaranteed to contain only  <code>\n</code> line endings</p>



<a name="178664122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178664122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ztlpn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178664122">(Oct 21 2019 at 15:06)</a>:</h4>
<p>Good to know, but I think in this case it doesn't matter? '\r' is whitespace too :)</p>



<a name="178664310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/folding%20in%20vscode/near/178664310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/folding.20in.20vscode.html#178664310">(Oct 21 2019 at 15:09)</a>:</h4>
<p>Yeah, it shouldn't matter</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>